/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : my_alim.js                            
*  Description  : 알림
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-07-16  김효순    Created.  
*******************************************************************************/

jQuery(function($){
	// 상단 링크
	function clsTopLink()
	{
		this._HTMLDom  = null;
		
		this._init();
	}
	
	clsTopLink.prototype = {
		_init: function(){
			var $layer = $("div#container article.depth2_tab");
			this._HTMLDom = {
				"$layer": $layer
			}			
			$layer.find("a").attr("id", "idPermissionLink");
		}
	}
	
	//일일 알림
	function clsToday(){
		this._HTMLDom = null;
		this.listData = null;
		
		this.a = null;
		this._init();
	}	
	
	clsToday.prototype = {
		_init: function(){
			var $layer = $("div#container div.my_today_alim");
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("tbody tr:first").clone(true)
			}			
			this.a = true;
			this.setList([]);
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setList: function(thisData){			
			this.listData = thisData;
			this.display();
		}
		, setMemberInfo: function(n, info, i){
			var $layer = this.$getLayer();
			var thisClass = this;
			i = i || "";
			
			var $o = $layer.find("tbody tr").eq(n).find("[data-param='닉네임" + i + "']");
			
			this.listData[n]["인자"]["회원정보"] = info;
			var d = this.listData[n]["인자"]["회원정보"];
			$o.hover(function(){
				_gf.showProfileLayer($(this), d, "left", 0, $(this).height());
			}, function(){
				_gf.closeProfileLayer();
			});
		}
		, display: function(){
			var $layer = this.$getLayer();
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			$layer.find("article, table").hide();
			
			if(this.listData.length){
				$layer.find("table").show();
				$layer.find("tbody").empty();
			
				for(var i=0;i<this.listData.length;++i){
					$layer.find("tbody").append($element.clone(true));	
				}
				
				$layer.find("tbody tr").each(function(n){
					var d = thisClass.listData[n];
					
					_gf.$getAlim({
						"템플릿코드": d["템플릿코드"]
						, "parent": this
						, "d": d
						, "n": n
						, "콜백": function($d, arg){
							thisClass.listData[n]["인자"] = arg["인자"];
							$(arg["parent"]).find("td:eq(0)").empty().append($d);
							
							var d = thisClass.listData[n]["인자"];
							
							for(var ii=0;ii<3;ii++){
								var iii = ii || "";
								
								if(d["회원아이디" + iii]){
									_gf.getMemberInfo({
										"아이디": d["회원아이디" + iii]
										, "iii": iii
										, "콜백": function(res, info, argg){
											thisClass.setMemberInfo(n, info, argg["iii"]);
										}
									});
								}
							}
						}
					});
					
					if(thisClass.a){
						$(this).find("td:eq(1)").html(d["경과시간"]);
					}else{
						$(this).find("td:eq(1)").html(d["등록정보"]["일자"]);
						$(this).find("td:eq(2)").html(d["등록정보"]["시간"]);
					}
				});
			}else{
				$layer.find("article").show();
			}
		}
	}
	
	//주간알림
	function clsWeek(){
		this._HTMLDom = null;
		this.listData = null;
		
		this.a = null;
		this._init();
	}
	
	clsWeek.prototype = {
		_init: function(){
			var $layer = $("div#container section div.my_k_alim:eq(0)");
			var $img = $layer.find("img[alt='수락']:first, img[alt='거부']:first");
			$layer.find("tr:first td:eq(0)").append($img);
			
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("tbody tr:first").clone(true)
			}						
			this.a = false;
			this.setList([]);
		}
		, $getLayer: clsToday.prototype.$getLayer
		, setList: clsToday.prototype.setList
		, setMemberInfo: clsToday.prototype.setMemberInfo
		, display: clsToday.prototype.display
	}
	
	//월간알림
	function clsMonth(){
		this._HTMLDom = null;
		this.listData = null;
		
		this.a = null;
		this._init();
	}
	
	clsMonth.prototype = {
		_init: function(){
			var $layer = $("div#container section div.my_k_alim:eq(1)");
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("tbody tr:first").clone(true)
			}			
			
			this.a = false;
			this.setList([]);
		}
		, $getLayer: clsToday.prototype.$getLayer
		, setList: clsToday.prototype.setList
		, setMemberInfo: clsToday.prototype.setMemberInfo
		, display: clsToday.prototype.display
	}
	
	// 서버통신
	function clsComm()
	{
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){

		}
	}
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		this.objTopLink = null;
		this.objToday = null;
		this.objWeek = null;
		this.objMonth = null;
		
		this._permitStart = null;
	}
	
	clsControl.prototype = {
		_init: function(){
			this._preload();
			
			this.objComm = new clsComm();
			this.objTopLink = new clsTopLink();
			this.objToday = new clsToday();
			this.objWeek = new clsWeek();
			this.objMonth = new clsMonth();
			
			this._beforeStart();
			this._bind();
			//this._start();
		}
		, _preload: function(){
			_gf.spinStart();
			
		}
		, _beforeStart: function(){
			var thisClass = this;
			this._permitStart = {
				
			}
			
			$.ajax({
				url: _ROOT + "/js/mypage_common.js"
				, dataType: "script"
				, async: false
				, success: function(){
					
				}
				, error: function(){
					alert("error");
				}
			});
			
			_util.executeThread(function(){
				for(var k in thisClass._beforeStart){
					if(!thisClass._beforeStart[k]) return false;
				}
				
				return (_gf.getMyInfoData());
			}, function(){
				_gf.spinEnd();
				thisClass._start();
			});
		}
		, _start: function(){
			var thisClass = this;
			
			// 오늘받은 알림
			_gf.getAlimList({
				"알림구분코드": "N"
				, "조회기간": 0
				, "상단알림여부": "N"
				, "서비스구분코드": ""
				, "콜백": function(ret){
					thisClass.finishTodayAlimList(ret);
				}
			});
			// 최근 알림
			_gf.getAlimList({
				"알림구분코드": "B"
				, "조회기간": 7
				, "상단알림여부": "N"
				, "서비스구분코드": ""
				, "콜백": function(ret){
					thisClass.finishWeekAlimList(ret);
				}
			});
			// 지난 알림
			_gf.getAlimList({
				"알림구분코드": "N"
				, "조회기간": 30
				, "상단알림여부": "N"
				, "서비스구분코드": ""
				, "콜백": function(ret){
					thisClass.finishMonthAlimList(ret);
				}
			});
		}
		, _bind: function(){
			var thisClass = this;
			
		}
		// 오늘받은 알림 완료
		, finishTodayAlimList: function(ret){
			this.objToday.setList(ret["결과값"]);
		}
		// 최근 알림 완료
		, finishWeekAlimList: function(ret){
			this.objWeek.setList(ret["결과값"]);
		}
		// 지난 알림 완료
		, finishMonthAlimList: function(ret){
			this.objMonth.setList(ret["결과값"]);
		}
	}
	
	document.objControl = new clsControl();
	document.objControl._init();
});
